# encoding=utf-8
import asyncio, aiohttp
from lxml import etree


def writ(segment):
    with open('text_file.txt', 'a', encoding= 'utf-8') as file:
        file.write(segment + '\n')


async def get_content(session, url):
    async with session.get(url) as resp:
        text_ = await resp.text()
        html = etree.HTML(text_)
        xpath = html.xpath("//div[@id='content']/div[@class='post f list-post']")
        for i in range(len(xpath)):
            indexs_text_ = xpath[i].xpath("./div[@class='indexs']/text()")[3].strip()
            writ(indexs_text_)


async def main(start_page, end_page):
    async with aiohttp.ClientSession() as session:
        for page_num in range(start_page, end_page + 1):
            url = f'http://jandan.net/page/{page_num}'
            # asyncio.create_task(get_content(session, url))
            await get_content(session, url)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(1, 2976))
